package com.hw.blog.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hw.blog.common.PaginationInfo;
import com.hw.blog.service.PmgrFaqService;


/**
 * 관리자 FAQ 관리
 * @author oracle
 *
 */
@Controller
public class PmgrFaqController extends BaseController{
	@Autowired
	private PmgrFaqService service;

	private Logger log = Logger.getLogger(getClass());
	
	/**
	 * 메뉴 목록 조회
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/faq/faqList", method = RequestMethod.GET)
	public ModelAndView faqList(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView();
		// 페이징 시작
		PaginationInfo page = new PaginationInfo();
		int currnetPageNo =1;
		if(null != vo.get("currentPageNo") && !"".equals(vo.get("currentPageNo"))){
			currnetPageNo = Integer.parseInt( vo.get("currentPageNo").toString());
		}
		if(null != vo.get("recordCountPerPage") && !"".equals(vo.get("recordCountPerPage"))){
			page.setRecordCountPerPage(Integer.parseInt( vo.get("recordCountPerPage").toString()));
		}
		
		page.setCurrentPageNo(currnetPageNo);
		page.setParamMap(vo);

		int iTotalCnt = service.faqListCnt(vo);
		page.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			vo.put("_page", page);
			mav.addObject("list",service.faqList(vo) );	
		}
		
		mav.addObject("_page", page);
		mav.addObject("paging", page.getAdmHtml("faqList.do"));
		
		
		return mav;
	}
	
	/**
	 * 수정하기
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/faq/faqEdit",method = RequestMethod.GET)
	public ModelAndView faqEdit(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView();

		if(null != vo.get("fq_seq") && !"".equals(vo.get("fq_seq") )){
			mav.addObject("map",service.faqEdit(vo) );
		}
		PaginationInfo page = new PaginationInfo();
		vo.remove("fq_seq");
		page.setParamMap(vo);
		
		mav.addObject("_page", page);
		
		return mav;
	}

	/**
	 * 수정및 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/faq/editProcess")
	public ModelAndView editProcess(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do?menu_seq="+vo.get("menu_seq"));

		if(null != vo.get("fq_seq") && !"".equals(vo.get("fq_seq") )){
			service.updateFaq(vo);
		}else{
			service.insertFaq(vo);
		}

		return mav;
	}
	
	
	/**
	 * 삭제 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/faq/deleteProcess")
	public ModelAndView deleteProcess(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do?menu_seq="+vo.get("menu_seq"));
		service.deleteProcess(vo);	
		return mav;
	}

}
